local gongao = fk.CreateSkill {
    name = "ym1__gongao",
}
Fk:loadTranslationTable{
    ["ym1__gongao"] = "功獒",
    [":ym1__gongao"] = "一名角色首次濒死时，你获得一个每轮结束时执行的无阶段回合，或为以此法获得的回合加入一个尚无的阶段。背水:伤害来源为你。",
    ["@ym1__gongao"] = "功獒",
    ["#ym1__gongao_turn"] = "功獒",
    ["ym1__phase_start"] = "准",
    ["ym1__phase_judge"] = "判",
    ["ym1__phase_draw"] = "摸",
    ["ym1__phase_play"] = "出",
    ["ym1__phase_discard"] = "弃",
    ["ym1__phase_finish"] = "结",

    ["$ym1__gongao1"] = "攻城拔寨，建功立业。",
    ["$ym1__gongao2"] = "恪尽职守，忠心事主。",
    ["$ym1__gongao3"] = "若国有难，吾当举义。",
}
gongao:addEffect(fk.EnterDying,{
    mute = true,
    can_trigger = function(self, event, target, player, data)
        if player:hasSkill(gongao.name) and target and not table.contains(player:getTableMark(gongao.name), target.id) then
            player.room:addTableMark(player, gongao.name, target.id)
            local dying_events =  player.room.logic:getEventsOfScope(GameEvent.Dying, 2, function (e)
                return e.data.who == target
            end, Player.HistoryGame)
            return #dying_events == 1 and dying_events[1].data == data
        end
    end,
    on_cost = function (self, event, target, player, data)
        local room = player.room
        local choices , all_choices = {},{"phase_start","phase_judge","phase_draw","phase_play","phase_discard","phase_finish","Cancel"}
        for _, v in ipairs(all_choices) do
            if player:getMark("ym1__gongao_phase") == 0 or not table.contains(player:getMark("ym1__gongao_phase"),v) then
                table.insert(choices,v)
            end
        end
        local str = "功獒：获得一个额外回合，或为额外回合加入一个尚无的阶段"
        if data then
            if data.damage and data.damage.from and data.damage.from.id == player.id then
                str = "功獒：获得一个额外回合，并为额外回合加入一个尚无的阶段"
                table.removeOne(choices,"Cancel")
                table.removeOne(all_choices,"Cancel")
            end
            event:setCostData(self, "Cancel")
            if ( player:getMark("ym1__gongao_turn") > 0 or (data.damage and data.damage.from and data.damage.from.id == player.id) ) and #choices > 1 then
                local result = room:askToChoice(player,{
                    choices = choices,
                    skill_name = gongao.name,
                    prompt = str,
                    all_choices = all_choices,
                })
                if result ~= "Cancel" then
                    event:setCostData(self, result)
                end
            end
            return true
        end
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local turns = {"phase_start","phase_judge","phase_draw","phase_play","phase_discard","phase_finish"}
        local arr = type(player:getMark("ym1__gongao_phase")) == "table" and player:getMark("ym1__gongao_phase") or {}
        if event:getCostData(self) == "Cancel" or (data.damage and data.damage.from and data.damage.from.id == player.id) then
            room:addPlayerMark(player,"ym1__gongao_turn",1)
        end
        local str = "x"..tostring(player:getMark("ym1__gongao_turn")).." "
        for _, v in ipairs(turns) do
            if v == event:getCostData(self) then
                table.insert(arr,v)
                table.sort(arr,function (a, b)
                    return Util.PhaseStrMapper(a) < Util.PhaseStrMapper(b)
                end)
            end
            if table.contains(arr,v) then
                str = str..Fk:translate("ym1__"..v)
            end
        end
        room:notifySkillInvoked(player,gongao.name,"support")
        player:broadcastSkillInvoke(gongao.name,math.random(1,2))
        room:setPlayerMark(player,"ym1__gongao_phase",arr)
        room:setPlayerMark(player,"@ym1__gongao",str)
    end,
})
gongao:addEffect(fk.RoundEnd,{
    frequency = Skill.Compulsory,
    anim_type = "offensive",
    can_trigger = function (self, event, target, player, data)
        return player:usedSkillTimes("ym1__gongao-turn", Player.HistoryRound) < player:getMark("ym1__gongao_turn")
    end,
    on_cost = Util.TrueFunc,
    on_use = function (self, event, target, player, data)
        player:addSkillUseHistory("ym1__gongao-turn",1)
        local phaseList = type(player:getMark("ym1__gongao_phase")) == "table" and player:getMark("ym1__gongao_phase") or {}
        player:broadcastSkillInvoke("ym1__gongao",3)
        table.insertTable(phaseList,{"phase_roundstart"})
        player:gainAnExtraTurn(true, "ym1__gongao", table.map(phaseList, Util.PhaseStrMapper))
    end,
})
gongao:addEffect(fk.TurnEnd,{
    frequency = Skill.Compulsory,
    anim_type = "offensive",
    can_trigger = function (self, event, target, player, data)
        if target ~= player or player:getCurrentExtraTurnReason() ~= "ym1__gongao" then
            return false
        end
        return player:usedSkillTimes("ym1__gongao-turn", Player.HistoryRound) < player:getMark("ym1__gongao_turn")
    end,
    on_cost = Util.TrueFunc,
    on_use = function (self, event, target, player, data)
        player:addSkillUseHistory("ym1__gongao-turn",1)
        local phaseList = type(player:getMark("ym1__gongao_phase")) == "table" and player:getMark("ym1__gongao_phase") or {}
        player:broadcastSkillInvoke("ym1__gongao",3)
        table.insertTable(phaseList,{"phase_roundstart"})
        player:gainAnExtraTurn(true, "ym1__gongao", table.map(phaseList, Util.PhaseStrMapper))
    end,
})
return gongao